package com.ruoyi.project.jiaqin.wanttobuy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.jiaqin.company.domain.Company;
import com.ruoyi.project.jiaqin.company.service.ICompanyService;
import com.ruoyi.project.jiaqin.followmember.domain.JiaqinFollowMember;
import com.ruoyi.project.jiaqin.followmember.service.IJiaqinFollowMemberService;
import com.ruoyi.project.jiaqin.quoted.mapper.QuotedPriceMapper;
import com.ruoyi.project.jiaqin.supplyclass.domain.SupplyClass;
import com.ruoyi.project.jiaqin.supplyclass.service.ISupplyClassService;
import com.ruoyi.project.jiaqin.val.domain.ClassAttrVal;
import com.ruoyi.project.jiaqin.val.service.IClassAttrValService;
import com.ruoyi.project.jiaqin.wanttobuy.domain.WantToBuy;
import com.ruoyi.project.jiaqin.wanttobuy.mapper.WantToBuyMapper;
import com.ruoyi.project.jiaqin.wanttobuy.service.IWantToBuyService;
import com.ruoyi.project.jiaqin.wanttobuycollect.domain.WantToBuyCollect;
import com.ruoyi.project.jiaqin.wanttobuycollect.mapper.WantToBuyCollectMapper;
import com.ruoyi.project.system.member.domain.Member;
import com.ruoyi.project.system.member.service.IMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 采购Service业务层处理
 *
 * @author admin
 * @date 2020-12-25
 */
@Service
public class WantToBuyServiceImpl implements IWantToBuyService
{
    @Autowired
    private WantToBuyMapper wantToBuyMapper;
    @Autowired
    private IClassAttrValService iClassAttrValService;
    @Autowired
    private QuotedPriceMapper quotedPriceMapper;
    @Autowired
    private IWantToBuyService wantToBuyService;
    @Autowired
    private ISupplyClassService supplyClassService;
    @Autowired
    private IJiaqinFollowMemberService jiaqinFollowMemberService;
    @Autowired
    private IMemberService memberService;
    @Autowired
    private WantToBuyCollectMapper wantToBuyCollectMapper;

    /**
     * 查询采购
     *
     * @param id 采购ID
     * @return 采购
     */
    @Override
    public WantToBuy selectWantToBuyById(String id)
    {
        WantToBuy wantToBuy = wantToBuyMapper.selectWantToBuyById(id);
        if(wantToBuy.getAttrVals()!= null && !wantToBuy.getAttrVals().equals("")){
            String[] srr = wantToBuy.getAttrVals().split(",");
            List<ClassAttrVal> list = new ArrayList<>();
            for (String s:srr) {
                ClassAttrVal classAttrVal = iClassAttrValService.selectClassAttrValById(s);
                list.add(classAttrVal);
            }
            wantToBuy.setAttrValList(list);
        }
        return wantToBuy;
    }

    /**
     * 查询采购列表
     *
     * @param wantToBuy 采购
     * @return 采购
     */
    @Override
    public List<WantToBuy> selectWantToBuyList(WantToBuy wantToBuy)
    {
        List<WantToBuy> list = wantToBuyMapper.selectWantToBuyList(wantToBuy);
        if(list != null && list.size()!=0){
            for (WantToBuy w:list) {
                List<ClassAttrVal> list1 = new ArrayList<>();
                if(w.getAttrVals()!= null&&!w.getAttrVals().equals("")){
                    String[] srr = w.getAttrVals().split(",");
                    for (String s:srr) {
                        ClassAttrVal classAttrVal = iClassAttrValService.selectClassAttrValById(s);
                        list1.add(classAttrVal);
                    }
                    w.setAttrValList(list1);
                }
            }
        }
        return list;
    }

    @Autowired
    private ICompanyService companyService;
    @Override
    public PageInfo<WantToBuy> selectWantToBuyListPageInfo(Integer pageNum, Integer pageSize, WantToBuy wantToBuy) {
//        PageHelper.startPage(pageNum,pageSize);
        if(wantToBuy.getSortField() == null || wantToBuy.getSortField().equals("")){
            PageHelper.startPage(pageNum,pageSize,"views desc");
        }else{
            PageHelper.startPage(pageNum,pageSize,wantToBuy.getSortField() + " "+wantToBuy.getSortType());
        }
        List<WantToBuy> list = wantToBuyMapper.selectWantToBuyList(wantToBuy);
        if(list != null && list.size()!=0){
            for (WantToBuy w:list) {
                List<ClassAttrVal> list1 = new ArrayList<>();
                if(w.getAttrVals()!= null&&!w.getAttrVals().equals("")){
                    String[] srr = w.getAttrVals().split(",");
                    for (String s:srr) {
                        ClassAttrVal classAttrVal = iClassAttrValService.selectClassAttrValById(s);
                        list1.add(classAttrVal);
                    }
                    w.setAttrValList(list1);
                }
                //设置报价数量
                w.setQuotedNum(quotedPriceMapper.selectQuotedPriceCountByWantToBuyId(w.getId()));
            }
            for (WantToBuy Wtb:list) {
                SupplyClass supplyClassThr = supplyClassService.selectSupplyClassById(Wtb.getThrClassId());
                if (supplyClassThr != null) {
                    WantToBuy want = new WantToBuy();
                    want.setId(Wtb.getId());
                    want.setThrClassName(supplyClassThr.getName());
                    Wtb.setThrClassName(want.getThrClassName());
                }
                Member memberById = memberService.selectMemberById(Wtb.getMemberId());
                // 是否是企业
                if (memberById != null) {
                    Company company = companyService.selectCompanyByMemberId(memberById.getId());
                    if (company != null) {
                        Wtb.setMemberName(company.getCompanyName());
                        Wtb.setHeadpic(company.getCompanyAvatar());
                        Wtb.setIsCompany(1);
                        Wtb.setMemberId(company.getId());
                    } else {
                        Wtb.setMemberName(memberById.getUsername());
                        Wtb.setHeadpic(memberById.getHeadpic());
                        Wtb.setIsCompany(0);
                        Wtb.setMemberId(memberById.getId());
                    }
                }
            }
        }
        PageInfo<WantToBuy> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 新增采购
     *
     * @param wantToBuy 采购
     * @return 结果
     */
    @Override
    public int insertWantToBuy(WantToBuy wantToBuy)
    {
        wantToBuy.setId(UUID.randomUUID().toString().replace("-",""));
        return wantToBuyMapper.insertWantToBuy(wantToBuy);
    }

    /**
     * 修改采购
     *
     * @param wantToBuy 采购
     * @return 结果
     */
    @Override
    public int updateWantToBuy(WantToBuy wantToBuy)
    {
        return wantToBuyMapper.updateWantToBuy(wantToBuy);
    }

    /**
     * 删除采购对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteWantToBuyByIds(String ids)
    {
        return wantToBuyMapper.deleteWantToBuyByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除采购信息
     *
     * @param id 采购ID
     * @return 结果
     */
    @Override
    public int deleteWantToBuyById(String id)
    {
        return wantToBuyMapper.deleteWantToBuyById(id);
    }

    @Override
    public PageInfo<WantToBuyCollect> getFocusOnBuying(Integer pageNum, Integer pageSize, String memberId) {
//        // 查询我关注的人
//        JiaqinFollowMember followMember = new JiaqinFollowMember();
//        followMember.setMemberId(memberId);
//        List<JiaqinFollowMember> followMemberList = jiaqinFollowMemberService.selectJiaqinFollowMemberList(followMember);
//
//        List<Object> arrayList = new ArrayList<>();
//
//        if (followMemberList != null && followMemberList.size() != 0) {
//            for (JiaqinFollowMember JFM : followMemberList) {
//                // 查询我关注的人发布的求购
//                PageHelper.startPage(pageNum,pageSize);
//                List<WantToBuy> wantToBuyList = wantToBuyMapper.getFocusOnBuying(JFM.getFollowMemberId());
//
//                if (wantToBuyList != null && wantToBuyList.size() != 0) {
//                    for (WantToBuy WTB : wantToBuyList) {
//                        // 关联我关注的人发布的求购信息
//                        WantToBuy wantToBuyById = wantToBuyMapper.selectWantToBuyById(WTB.getId());
//                        if (wantToBuyById != null) {
//                            SupplyClass supplyClassByTwo = supplyClassService.selectSupplyClassById(wantToBuyById.getTwoClassId());
//                            SupplyClass supplyClassByThr = supplyClassService.selectSupplyClassById(wantToBuyById.getThrClassId());
//                            ClassAttrVal classAttrVal = iClassAttrValService.selectClassAttrValById(wantToBuyById.getAttrVals());
//                            if (supplyClassByTwo != null) {
//                                WTB.setTwoClassName(supplyClassByTwo.getName());
//                            }
//                            if (supplyClassByThr != null) {
//                                WTB.setThrClassName(supplyClassByThr.getName());
//                            }
//                            if (classAttrVal != null) {
//                                WTB.setAttrVals(classAttrVal.getAttrVal());
//                            }
//                            Member memberById = memberService.selectMemberById(wantToBuyById.getMemberId());
//                            if (memberById != null) {
//                                WTB.setMemberName(memberById.getUsername());
//                            }
//                        }
//                    }
//                }
//                arrayList.add(wantToBuyList);
//            }
//        }
//        PageInfo<Object> pageInfo = new PageInfo<>(arrayList);
//        return pageInfo;
        PageHelper.startPage(pageNum,pageSize);
        List<WantToBuyCollect> list = wantToBuyCollectMapper.selectWantToBuyCollectByMemberId(memberId);
        if(list != null && list.size()!= 0){
            for (WantToBuyCollect WTBC :list) {
                WantToBuy wantToBuyById = wantToBuyService.selectWantToBuyById(WTBC.getWantToBuyId());
                if(wantToBuyById != null) {
                    Member memberById = memberService.selectMemberById(wantToBuyById.getMemberId());
                    WTBC.setWantToBuy(wantToBuyById);
                    if (memberById != null) {
                        wantToBuyById.setMemberName(memberById.getUsername());
                        wantToBuyById.setHeadpic(memberById.getHeadpic());
                    }
                }
            }
        }
        PageInfo<WantToBuyCollect> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public WantToBuy findWantToBuyById(String id) {
        return wantToBuyMapper.findWantToBuyById(id);
    }

    @Override
    public PageInfo<WantToBuy> getWantToBuyListByMemberId(Integer pageNum, Integer pageSize, String memberId) {
        PageHelper.startPage(pageNum,pageSize);
        List<WantToBuy> list = wantToBuyMapper.getWantToBuyListByMemberId2(memberId);
        List<WantToBuy> newList = new ArrayList<WantToBuy>();
        Set<String> set = new HashSet<String>();
        for (WantToBuy WTB : list) {
            String userId = WTB.getMemberId();
            if (!set.contains(userId)) { //set中不包含重复的
                set.add(userId);
                Company company = companyService.selectCompanyByMemberId(WTB.getMemberId());
                Member member = memberService.selectMemberById(WTB.getMemberId());
                if (company != null) {
                    WTB.setIsCompany(1);
                    WTB.setMemberName(company.getCompanyName());
                    WTB.setHeadpic(company.getCompanyAvatar());
                } else {
                    WTB.setIsCompany(0);
                    WTB.setMemberName(member.getUsername());
                    WTB.setHeadpic(member.getHeadpic());
                }
                    WTB.setMemberCompanyId(WTB.getMemberId());
                    newList.add(WTB);
                }
            // 关注求购列表每一条数字提示
            JiaqinFollowMember followMember = jiaqinFollowMemberService.selectJiaqinFollowMemberByMemberIdAndToMemberId(memberId, userId);
            if (followMember != null && followMember.getWantToBuyNum() == 1) {
                List<WantToBuy> list1 = wantToBuyMapper.getWantToBuyNumByMemberId(userId);
                WTB.setWantToBuyNum(list1.size());
            } else {
                WTB.setWantToBuyNum(0);
            }
        }
        PageInfo<WantToBuy> pageInfo = new PageInfo<>(newList);
        return pageInfo;
    }

    @Override
    public List<WantToBuy> getWantToBuyListByMemberId2(String memberId) {
        return wantToBuyMapper.getWantToBuyListByMemberId2(memberId);
    }

    @Override
    public PageInfo<WantToBuy> getFocusOnBuyingByFollowMemberId(Integer pageNum, Integer pageSize, String followMemberId) {
        PageHelper.startPage(pageNum,pageSize);
        WantToBuy wantToBuy = new WantToBuy();
        wantToBuy.setMemberId(followMemberId);
        List<WantToBuy> list = wantToBuyMapper.selectWantToBuyList(wantToBuy);
        for (WantToBuy WTB : list) {
                Company company = companyService.selectCompanyByMemberId(WTB.getMemberId());
                Member member = memberService.selectMemberById(WTB.getMemberId());
                if (company != null) {
                    WTB.setIsCompany(1);
                    WTB.setMemberName(company.getCompanyName());
                    WTB.setHeadpic(company.getCompanyAvatar());
                } else {
                    WTB.setIsCompany(0);
                    WTB.setMemberName(member.getUsername());
                    WTB.setHeadpic(member.getHeadpic());
                }
                WTB.setMemberCompanyId(WTB.getMemberId());

            // 取消关注供应数字提示
            JiaqinFollowMember followMember = new JiaqinFollowMember();
            followMember.setFollowMemberId(WTB.getMemberId());
            List<JiaqinFollowMember> followMemberList = jiaqinFollowMemberService.selectJiaqinFollowMemberList(followMember);
            if (followMemberList.size() > 0) {
                for (JiaqinFollowMember JFM : followMemberList) {
                    JFM.setId(JFM.getId());
                    JFM.setWantToBuyNum(0);
                    jiaqinFollowMemberService.updateJiaqinFollowMember(JFM);
                }
            }

        }
        PageInfo<WantToBuy> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
}
